﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AutoGear.BLL;
using ECommerce.Library.DataStructure;
using ECommerce.Library.Web;
using ECommerce.Library;
using System.Collections.Generic;
using eShop.Model;
using eShop.Business;

public partial class controls_wucSubCategory : BaseControl
{

    private int _parentCategoryId;

    public int ParentCategoryId
    {
        get { return this._parentCategoryId; }
        set { this._parentCategoryId = value; }
    }

    private int _currentPageIndex;
    private int _pageSize;
    private int _totalPages;
    private string _sortOder;

    private string _searchProductName;
    public string SearchProductName
    {
        get { return this._searchProductName; }
        set { this._searchProductName = value; }
    }

    private string _searchItemNumber;
    public string SearchItemNumber
    {
        get { return this._searchItemNumber; }
        set { this._searchItemNumber = value; }
    }

    private int _repeatColumns = 2; //3
    public int RepeatedColumns
    {
        get { return this._repeatColumns; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                //default parameter
                if ((!string.IsNullOrEmpty(Request.QueryString["view"])) && (Request.QueryString["view"] == "all"))
                {
                    this._pageSize = 99999;
                }
                else
                {
                    this._pageSize = 20;
                }
                this._currentPageIndex = 0;
                //SetPagingParameter(1);

                LoadData();
            }
        }
        catch (Exception ex)
        {

            throw ex;
        }

    }

    private void LoadData()
    {
        LoadCategoryBanner();
        LoadRepeaterItems();
    }

    private void LoadCategoryBanner()
    {
        Category cat = CategoryManager.GetById(this.ParentCategoryId);

        if (cat != null)
        {
            //aTagCategoryUrl.HRef = Page.ResolveUrl(cat.CategoryUrl);
            //imgCategoryBanner.ImageUrl = ImageResizer.CheckImageMissing(Page.ResolveUrl(cat.CategoryBannerImageUrl.Replace("big", "noproduct")), AutoGear.BLL.SiteSettings.NoImageMaxSizeUrl);
            imgCategoryBanner.ImageUrl = ImageResizer.CheckImageMissing(Page.ResolveUrl(cat.CategoryBannerImageUrl), AutoGear.BLL.SiteSettings.NoImageMaxSizeUrl);
        }

    }

    protected void LoadRepeaterItems()
    {

        //rptSiteMap.DataSource = this.GetDefaultSiteMap();
        //rptSiteMap.DataBind();

        //rptSiteMap2.DataSource = this.GetDefaultSiteMap();
        //rptSiteMap2.DataBind();

        rptSiteMap.DataSource = CategoryManager.GetSubCategorySiteMapTree(SiteUtility.SuperRootCategoryId, ParentCategoryId);
        rptSiteMap.DataBind();

        rptSiteMap2.DataSource = CategoryManager.GetSubCategorySiteMapTree(SiteUtility.SuperRootCategoryId, ParentCategoryId);
        rptSiteMap2.DataBind();

        Category ParentCategory = CategoryManager.GetById(_parentCategoryId);
        ReadWriteCustomCollection<Category> subCategories = CategoryManager.GetActiveSubCategoriesPaged(ParentCategory.CategoryId, "DisplayOrder", _currentPageIndex, _pageSize);

        ReadWriteCustomCollection<Category> tempSubCategories;
        List<ReadWriteCustomCollection<Category>> list = new List<ReadWriteCustomCollection<Category>>();
        tempSubCategories = new ReadWriteCustomCollection<Category>();
        int count = 0;
        foreach (Category subCategory in subCategories)
        {
            tempSubCategories.Add(subCategory);
            count++;
            if (count % RepeatedColumns == 0)
            {
                list.Add(tempSubCategories);
                tempSubCategories = new ReadWriteCustomCollection<Category>();
            }
        }
        if (tempSubCategories.Count > 0)
        {
            list.Add(tempSubCategories);
        }

        rptVertical.DataSource = list;
        rptVertical.DataBind();

        int totalItems=0;

        Category ParentCategory2 = CategoryManager.GetById(_parentCategoryId);
        totalItems = ParentCategory2.ActiveSubCategoriesCount;

        this._totalPages = (int)Math.Ceiling(totalItems / (double)this._pageSize);
        int firstItem = (this._currentPageIndex - 1) * this._pageSize;
        int lastItem = this._currentPageIndex * this._pageSize - 1;

        if (lastItem >= totalItems)
            lastItem = totalItems - 1;
        WritePagingData();
        BindPager();

        //LoadPager(totalItems, CurrentIndex, PageSize, "Categories");
       

    }

    private void BindPager()
    {
        List<int> pagingList = new List<int>();

        for (int i = 0; i < this._totalPages; i++)
        {
            pagingList.Add(i + 1);
        }
        rptPaging.DataSource = pagingList;
        rptPaging.DataBind();

        rptPaging2.DataSource = pagingList;
        rptPaging2.DataBind();
    }



    protected void ShowEmptyMessage()
    {
        uccResponseMessage.SetFailMessage("No Category Listed");
    }

    protected void rptSiteMap_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HyperLink hlkSiteMap = (HyperLink)e.Item.FindControl("hlkSiteMap");
                KeyValuePair<string, string> node = (KeyValuePair<string, string>)e.Item.DataItem;
                hlkSiteMap.NavigateUrl = node.Value;
                hlkSiteMap.Text = node.Key.ToLower() == "none" ? "Home" : node.Key;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void rptSiteMap2_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HyperLink hlkSiteMap = (HyperLink)e.Item.FindControl("hlkSiteMap");
                KeyValuePair<string, string> node = (KeyValuePair<string, string>)e.Item.DataItem;
                hlkSiteMap.NavigateUrl = node.Value;
                hlkSiteMap.Text = node.Key.ToLower() == "none" ? "Home" : node.Key;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected ReadWriteCustomCollection<KeyValuePair<string, string>> GetDefaultSiteMap()
    {
        ReadWriteCustomCollection<KeyValuePair<string, string>> node = new ReadWriteCustomCollection<KeyValuePair<string, string>>();
        node.Add(new KeyValuePair<string, string>("Home", AutoGear.BLL.CustomSiteMap.Home));
        node.Add(new KeyValuePair<string, string>("Gift Set", AutoGear.BLL.CustomSiteMap.Home));
        return node;
    }


    protected void Vertical_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

        try
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //ProductCategory subCategory = (ProductCategory)e.Item.DataItem;
                //Label hlkSubCategoryName = (Label)e.Item.FindControl("lblSubCategoryName");
                //if (hlkSubCategoryName != null)
                //{
                //    hlkSubCategoryName.Text = subCategory.CategoryUrl;
                //}
                //hlkSubCategoryName.Text = subCategory.CategoryName;
                //HyperLink hlkImgSubCategory = (HyperLink)e.Item.FindControl("hlkImgSubCategory");
                //hlkImgSubCategory.NavigateUrl = subCategory.CategoryUrl;
                //Image imgSubCategory = (Image)e.Item.FindControl("imgSubCategory");
                //imgSubCategory.ToolTip = subCategory.CategoryName;
                //imgSubCategory.ImageUrl = ImageResizer.CheckImageMissing(subCategory.CategoryImageUrl, AutoGear.BLL.SiteSettings.NoImageLargeThumbnailUrl);
                //string imgUrl = imgSubCategory.ImageUrl.Replace(SiteSettings.SiteBaseUrl, "~");
                //System.Drawing.Size imageSize = ImageResizer.GetImageDimension(imgUrl);
                //if (imageSize.Height > 129)
                //{
                //    Unit imageHeight = new Unit(129);
                //    imgSubCategory.Height = imageHeight;
                //}
               

                Repeater rptHorizontal = (Repeater)e.Item.FindControl("rptHorizontal");
                rptHorizontal.DataSource = (ReadWriteCustomCollection<Category>)e.Item.DataItem;
                rptHorizontal.DataBind();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void Horizontal_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Category subCategory = (Category)e.Item.DataItem;
                Label hlkSubCategoryName = (Label)e.Item.FindControl("lblSubCategoryName");
                if (hlkSubCategoryName != null)
                {
                    hlkSubCategoryName.Text = subCategory.CategoryUrl;
                }
                hlkSubCategoryName.Text = subCategory.CategoryName;
                HyperLink hlkImgSubCategory = (HyperLink)e.Item.FindControl("hlkImgSubCategory");
                hlkImgSubCategory.NavigateUrl = subCategory.CategoryUrl;
                Image imgSubCategory = (Image)e.Item.FindControl("imgSubCategory");
                imgSubCategory.ToolTip = subCategory.CategoryName;
                imgSubCategory.ImageUrl = ImageResizer.CheckImageMissing(subCategory.CategoryImageUrl, AutoGear.BLL.SiteSettings.NoImageLargeThumbnailUrl);
                string imgUrl = imgSubCategory.ImageUrl.Replace(SiteSettings.SiteBaseUrl, "~");
                System.Drawing.Size imageSize = ImageResizer.GetImageDimension(imgUrl);
                if (imageSize.Height > 129)
                {
                    Unit imageHeight = new Unit(129);
                    imgSubCategory.Height = imageHeight;
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void rptHorizontal_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
    }

    protected void rptPaging_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                int pageIndex = (int)e.Item.DataItem;
                LinkButton lkbPageIndex = (LinkButton)e.Item.FindControl("lkbPageIndex");
                if (_currentPageIndex == pageIndex)
                {
                    DisableLinkButtonStyle(lkbPageIndex);
                }
                else
                {
                    EnableLinkButtonStyle(lkbPageIndex);
                }
                lkbPageIndex.Text = pageIndex.ToString();
                lkbPageIndex.CommandArgument = pageIndex.ToString();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void rptPaging_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            LinkButton lkbPageIndex = (LinkButton)e.CommandSource;
            ReadPagingData();
            this._currentPageIndex = int.Parse(lkbPageIndex.CommandArgument);
            WritePagingData();
            LoadRepeaterItems();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void EnableLinkButtonStyle(LinkButton lkb)
    {
        lkb.Style["color"] = "#6B1B16"; //"#535353";
        lkb.Style["cursor"] = "pointer";
        lkb.OnClientClick = "javascript:return true;";
        lkb.Style["text-decoration"] = "none";
    }

    protected void DisableLinkButtonStyle(LinkButton lkb)
    {
        lkb.Style["color"] = "#6B1B16"; //"#B0B0B0";
        lkb.Style["cursor"] = "default";
        lkb.OnClientClick = "javascript:return false;";
        lkb.Style["text-decoration"] = "none";
    }

    private void ReadPagingData()
    {
        this._currentPageIndex = Convert.ToInt32(ViewState["CurrentPageIndex"]);
        this._pageSize = Convert.ToInt32(ViewState["PageSize"]);
        this._totalPages = Convert.ToInt32(ViewState["TotalPages"]);
        this._sortOder = Convert.ToString(ViewState["SortOrder"]);
        this._searchItemNumber = Convert.ToString(ViewState["SearchItemNumber"]);
        this._searchProductName = Convert.ToString(ViewState["SearchProductName"]);
        this._parentCategoryId = Convert.ToInt32(ViewState["CategoryId"]);
    }

    private void WritePagingData()
    {
        ViewState["CurrentPageIndex"] = this._currentPageIndex;
        ViewState["PageSize"] = this._pageSize;
        ViewState["TotalPages"] = this._totalPages;
        ViewState["SortOrder"] = this._sortOder;
        ViewState["SearchItemNumber"] = this._searchItemNumber;
        ViewState["SearchProductName"] = this._searchProductName;
        ViewState["CategoryId"] = this._parentCategoryId;
    }

    protected void rptPaging2_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                int pageIndex = (int)e.Item.DataItem;
                LinkButton lkbPageIndex = (LinkButton)e.Item.FindControl("lkbPageIndex");
                if (_currentPageIndex == pageIndex)
                {
                    DisableLinkButtonStyle(lkbPageIndex);
                }
                else
                {
                    EnableLinkButtonStyle(lkbPageIndex);
                }
                lkbPageIndex.Text = pageIndex.ToString();
                lkbPageIndex.CommandArgument = pageIndex.ToString();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void rptPaging2_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            LinkButton lkbPageIndex = (LinkButton)e.CommandSource;
            ReadPagingData();
            this._currentPageIndex = int.Parse(lkbPageIndex.CommandArgument);
            WritePagingData();
            LoadRepeaterItems();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }



}
